package com.cmy.framework_base.app

import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.text.TextUtils
import com.cmy.framework_base.activity.ActivityContext

class AppEnvImpl : AppEnvironment {

    private var mTestChannel: Boolean? = null
    private var mStartUpTime: Long = -1
    private var APP_MAX_MEMORY_MB = ""

    companion object{
        private val sAppEnvironment: AppEnvironment = AppEnvImpl()

        fun getInstance(): AppEnvironment {
            return sAppEnvironment
        }
    }




    override fun getAppContext(): Application? = AppEnv.APP

    override fun getCurrentContext(): Context? {
        val activity: Activity? = ActivityContext.getInstance().getCurrentActivity()
        if (activity != null) {
            return activity
        }
        return  AppEnv.APP
    }

    override fun getAppMaxMemory(): String {
        if (TextUtils.isEmpty(APP_MAX_MEMORY_MB)) {
            val am = AppEnv.APP!!.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            APP_MAX_MEMORY_MB = am.memoryClass.toString()
        }
        return APP_MAX_MEMORY_MB
    }

    override fun isTestChannel(): Boolean {
        return false
    }

    override fun isGooglePlayChannel(): Boolean {
       return "google_play".equals(AppEnv.CHANNEL, ignoreCase = true)
    }

    override fun setAppStartUpTime(time: Long) {
        mStartUpTime = time
    }

    override fun getAppStartUpTime(): Long {
        return mStartUpTime
    }
}